#ifndef _COMM_SINGLETON_H_
#define _COMM_SINGLETON_H_


template <typename T>
class Singleton
{
public:
    struct object_creator
    {
        object_creator() { Singleton<T>::instance(); }
        inline void do_nothing()const {}
    };

    static object_creator create_object;

    typedef T object_type;

    static object_type& instance()
    {
        static object_type obj;
        create_object.do_nothing();
        return obj;
    }
};

template <typename T> typename Singleton<T>::object_creator
Singleton<T>::create_object;


#define PATTERN_SINGLETON_DECLARE(classname)     \
static classname * instance(int free = 0)          \
{                                                 \
    static classname * _instance = NULL;          \
    if(0 == free)\
    {\
    if(( NULL == _instance)&&(free != 1))\
    {\
        _instance = new classname;\
    }\
    }\
    else\
    {\
        if(NULL != _instance)\
        {\
            delete _instance;\
            _instance = NULL;\
        }\
    }\
    return _instance;\
};

#endif